
var personnelleave = angular.module("personnelleave", ['angularFileUpload', 'vModal', 'tm.pagination', 'ngAnimate', 'loadModule', 'PensonSelector', 'dropdownApp', 'btnconqitk', 'nstdirective', 'nspermirective'])
 .factory('addUserFactory', function (vModal) {
     return vModal({
         controller: 'addUserModal',
         controllerAs: 'addUserFactory',
         templateUrl: '/system/views/personnel/newlyadded.html'
     });
 })
//人员首页
.controller('personnelleavecontroller', ['$scope', '$rootScope', '$stateParams', '$rootScope', function ($scope, $rootScope, $stateParams, $rootScope) {
    $scope.btnlist = { savefun: false, addinfo: false, editinfo: false, delinfo: false };
    if ($stateParams.id == "") {
        $rootScope.currentTab = 0;
    } else {
        $rootScope.currentTab = $stateParams.id;    //默认页面地址rootscopepathplay 路由名称     
    }

    if ($stateParams.routename == "") {
        $rootScope.btnTab = "index.personnelmanagement";
    } else {
        $rootScope.btnTab = $stateParams.routename;
    }

    $scope.childobj = function () {
        var obj;
        angular.forEach($rootScope.rootscopeauthority, function (data, index, array) {
            if (data.id == '5b820605e133c5dd106335caec6fdfac') {
                obj = data;
            }
        });
        return obj;
    }


    $scope.personnelauthority = $scope.childobj().childroute;   // 人员路由控制    

    //修改选中样式
    $scope.isActiveTab = function (tabUrl) {
        if ($scope.currentTab == "HOME" && tabUrl == 0) {
            return true;
        } else {
            return tabUrl == $scope.currentTab;
        }
    }
} ])

// 离职列表页面
.controller('personnelILcontroller', ['$scope', '$rootScope', 'addUserFactory', '$timeout', '$location', '$http', '$stateParams', 'FileUploader', 'MsgCon', 'Msg', 'RequestPopService', 'sysUserInfo', '$state', function 
($scope, $rootScope, addUserFactory, $timeout, $location, $http, $stateParams, FileUploader, MsgCon, Msg, RS, sysUserInfo, $state) {

    $scope.btnlist = { addinfo: false, editinfo: false, delinfo: false };
    $scope.personToken = sysUserInfo.getuserinfotoken();
    $scope.lvid = sysUserInfo.getuserinfo().powerLV;
    $scope.sendFlag = false; // 自动通知变量
    $scope.userurl = $rootScope.userurl;
    //********自动通知点击事件
    $scope.AutoNotification = function () {
        $scope.sendFlag = !$scope.sendFlag;
        lorem.sendFlag = $scope.sendFlag;
    }
    $scope.addcomplexnum = function (num) {
        return parseInt(num) > 9 ? num : "0" + num;
    }

    var lorem = { title: '', sendFlag: false, objinfo: null };
    //*********HOME 调用过来的**********
    if ($stateParams.id == "HOME") {
        lorem.title = ""; addUserFactory.activate(lorem);
        $stateParams.id = "";
    }
    /** 人员列表复选框 ** 开始 **/
    $scope.user = { userinfo: [] };
    //checkbox全选
    $scope.checkAll = function () {
        var a = document.getElementById("checkall")
        if ($scope.user.userinfo.length == $scope.names.length) {
            $scope.uncheckAll();
            a.checked = false;
        } else {
            $scope.user.userinfo = angular.copy($scope.names);
            a.checked = true;
        }
    };
    //checkbox取消全选
    $scope.uncheckAll = function () {
        $scope.user.userinfo = [];
    };

    $scope.checkFirst = function () {
        $scope.user.userinfo.splice(0, $scope.user.userinfo.length);
        $scope.user.userinfo.push('guest');
    };
    /** 人员列表复选框 ** 结束 **/


    $scope.menudate = $rootScope.rootscopeauthority;
    $scope.addflag = false; //新增的显示隐藏  

    //添加人员 panel
    $scope.addinfo = function () {
        $scope.addflag = true;
        $scope.personneladdstyle = { "height": document.documentElement.clientHeight - 90 + "px" };
        //$scope.adduser();
    }

    //***************************************筛选开始*********************************************/

    var searchTypeList = [1];
    $scope.playparams = {
        searchType: "1",
        searchText: "",
        startTime: "",
        endTime: "",
        startDate: "",
        endDate: "",
        pageIndex: 1,
        pageSize: 0,
        organization_ID: sysUserInfo.getuserinfo().organization_ID,
        userId: sysUserInfo.getuserinfo().user_ID,
        powerLV: sysUserInfo.getuserinfo().powerLV
    };

    $scope.GetTime = function () {
        $scope.playparams.startTime = document.getElementById("startTime").value;
        $scope.playparams.endTime = document.getElementById("endTime").value;
    }

    $scope.GetDate = function () {
        $scope.playparams.startDate = document.getElementById("startDate").value;
        $scope.playparams.endDate = document.getElementById("endDate").value;
    }
    //处理下拉框选择的内容 
    $scope.placeholder = "请输入姓名支持模糊查询";
    $scope.optionCheck = function (TypeIndex) {
        $scope.playparams.searchText = "";
        TypeIndex = parseInt(TypeIndex);
        switch (TypeIndex) {
            case 1:
                $scope.placeholder = "请输入姓名支持模糊查询";
                break;
            case 2:
                $scope.placeholder = "请输入邮箱支持模糊查询";
                break;
            case 3:
                $scope.placeholder = "请输入电话号码支持模糊查询";
                break;
            case 10:
                $scope.placeholder = "请输入登录账号支持模糊查询";
                break;
        }
        //类型1 2 3 4 8 9 不可共存
        if (TypeIndex == 1 || TypeIndex == 2 || TypeIndex == 3 || TypeIndex == 4 || TypeIndex == 8 || TypeIndex == 9 || TypeIndex == 10) {
            for (var i = 1, length1 = 4; i <= length1; i++) {
                var isHas = $scope.containsObj(searchTypeList, i);
                if (isHas > -1) {
                    searchTypeList.splice(isHas, 1);
                }
            }
            var isHas = $scope.containsObj(searchTypeList, 8);
            if (isHas > -1) {
                searchTypeList.splice(isHas, 1);
            }
            isHas = $scope.containsObj(searchTypeList, 9);
            if (isHas > -1) {
                searchTypeList.splice(isHas, 1);
            }
            isHas = $scope.containsObj(searchTypeList, 10);
            if (isHas > -1) {
                searchTypeList.splice(isHas, 1);
            }
        }
        searchTypeList.push(TypeIndex);
    }
    //判断数组中是否包含此元素
    $scope.containsObj = function (arr, obj) {
        try {
            var i = arr.length;
            while (i--) {
                if (arr[i] === obj) {
                    return i;
                }
            }
            return -1;
        } catch (e) {
            // statements
            return -1;
        }
    }
    // 回车查询
    $scope.myKeyup = function (e) {
        var keycode = window.event ? e.keyCode : e.which;
        if (keycode == 13) {
            $scope.AddSelect();
        }

    }
    // 调用查询
    $scope.AddSelect = function () {
        $scope.playparams.searchType = searchTypeList.join(',');
        //            if($scope.checkedflag){
        //                $scope.GetUserList("",0); 
        //            }else{
        //                $scope.playGetList();
        //            }
        $scope.GetUserList("", 0);
    }

    //--获取用户列表 -- 监听页面上有变动则进进入
    // -- obj:组织机构 用户组 角色
    // -- searchT： 5:组织机构 6:用户组 7:角色                
    var searchTypeListNew = [];
    $scope.param = {};
    $scope.GetUserList = function (Obj, searchT) {
        // [数组]1:姓名搜索或拼音简写模糊搜索,2:邮箱搜索,3:手机号搜索,4:按时间段搜索,                         
        //            if(searchT == 110){           
        searchTypeListNew = [];
        //            }
        var listparams = { // 请求参数
            dimissionUserName: "",
            dimissionUserPhone: "",
            dimissionUserEmail: "",
            startDate: $scope.playparams.startTime,
            endDate: $scope.playparams.endTime,
            pageIndex: $scope.playparams.pageIndex,
            pageSize: $scope.playparams.pageSize,
            dimissionUserFromOrgId: sysUserInfo.getuserinfo().organization_ID,
            userId: sysUserInfo.getuserinfo().user_ID
        };
        //用户名
        if ($scope.playparams.searchType == 1 || $scope.playparams.searchType == '1') {
            listparams.dimissionUserName = $scope.playparams.searchText;
            //手机号
        } else if ($scope.playparams.searchType == 3 || $scope.playparams.searchType == '3') {
            listparams.dimissionUserPhone = $scope.playparams.searchText;
            //邮箱
        } else if ($scope.playparams.searchType == 2 || $scope.playparams.searchType == '2') {
            listparams.dimissionUserEmail = $scope.playparams.searchText;
        }
        $scope.param = listparams;
        if (!$scope.loadingflag) // 控制不请求完不让请求
            $scope.playGetList();

    }
    $scope.personList = [];
    $scope.loadingflag = false;
    $scope.playGetList = function () {
        $scope.loadingflag = true;
        $scope.succ = false;
        $scope.promise = RS.findDimissionRecordInfo($scope.param).then(function (data) {
            $scope.loadingflag = false;
            $scope.succ = true;
            if (data.errorcode == 0) {// 成功
                // $scope.checkAll();
                if (data.datas == undefined || data.datas.length == 0) {
                    $scope.names = [];
                } else {
                    $scope.names = data.datas;
                }
                $scope.user.userinfo = [];
                $scope.paginationConf.totalItems = data.numCount;
                $scope.paginationConf.currentPage = data.pageIndex;
            }
        }, function callback() {
            $scope.loadingflag = false;
            //$scope.promise = -1;
        });

    }

    // *******************************筛选结束************************************* 
    //删除人员信息
    $scope.delinfo = function () {
        $scope.userid = "";
        if ($scope.user.userinfo.length > 0) {
            $scope.loadingflag = true;
            for (var i = 0; i < $scope.user.userinfo.length; i++) {
                if (i == 0) {
                    $scope.userid = $scope.user.userinfo[i].dimissionId;
                } else {
                    $scope.userid += "," + $scope.user.userinfo[i].dimissionId;
                }
            }
            MsgCon.delnoicon().then(function (flag) {   // 删除弹框
                if (flag) {
                    RS.deleteDimissionUser({ dimissionIds: $scope.userid, loginUserId: sysUserInfo.getuserinfo().user_ID }).then(function (data) {
                        $scope.loadingflag = false;
                        $scope.user.userinfo = [];
                        if (data.errorcode == 0) {
                            $scope.GetUserList("", 0);
                            Msg.success("删除成功！");
                        } else {
                            Msg.error("删除失败！");
                        }
                        var a = document.getElementById("checkall")
                        a.checked = false;
                    }, function errorCallback() {
                        //console.log("连接服务器失败！");
                        $scope.loadingflag = false;
                    });
                } else {
                    $scope.loadingflag = false;
                }
            }, function (p) { console.log('error') });
        } else {
            Msg.info('请选择一条记录！');
        }
    }

    $scope.addhide = function () {
        $scope.addflag = false;
    }
    $scope.adduser = function () { lorem.title = ""; addUserFactory.activate(lorem); };

    $scope.fuzhi = function () {

        $scope.userid = [];
        if ($scope.user.userinfo.length > 0) {
            $scope.loadingflag = true;
            for (var i = 0; i < $scope.user.userinfo.length; i++) {
                $scope.userid.push($scope.user.userinfo[i].dimissionId);
            }

            RS.conductRestoration({ dimissionUserIds: $scope.userid, userId: sysUserInfo.getuserinfo().user_ID }).then(function (data) {
                $scope.loadingflag = false;
                if (data.errorcode == 0) {
                    Msg.success("修改成功！");
                    $scope.GetUserList("", 0);
                } else if (data.errorcode == 2) {
                    Msg.error("账号重复", data.errormsg);
                } else {
                    Msg.error("修改失败！");
                }
            }, function errorCallback() {
                console.log("连接服务器失败！");
                $scope.loadingflag = false;
            });

        } else {
            Msg.info('请选择一条记录！');
        }
    }


    //$scope.index1 = 1;
    $scope.succ = true;
    $scope.paginationConf = {
        currentPage: 1, //$location.search().currentPage ? $location.search().currentPage :
        totalItems: 0,
        itemsPerPage: 10,
        //pagesLength: 10,
        perPageOptions: [10, 20],
        //rememberPerPage: 'perPageItems',
        onChange: function () {
            if (!$scope.succ) {
                Msg.info("正在查询中！");
                return;
            } else {
                if ((parseInt($scope.playparams.pageIndex) != parseInt($scope.paginationConf.currentPage)) || (parseInt($scope.playparams.pageSize) != parseInt($scope.paginationConf.itemsPerPage))) {
                    if ($scope.paginationConf.currentPage == 0) { $scope.paginationConf.currentPage = 1; }
                    $scope.playparams.pageIndex = $scope.paginationConf.currentPage;
                    $scope.playparams.pageSize = $scope.paginationConf.itemsPerPage;
                    // $scope.playGetList();
                    $scope.GetUserList("", 0);
                }
            }
        }
    }

    $scope.loaddata = function () {

    }

    //控制图标对应提示黑框的隐藏/显示
    $scope.showMsg = function (titletype) {
        if (titletype == "1") {
            $scope.showPerson = true;
        } else if (titletype == "2") {
            $scope.showComplex = true;
        } else if (titletype == "3") {
            $scope.autnotice = true;
        }
    }

    $scope.hideMsg = function (titletype) {
        if (titletype == "1") {
            $scope.showPerson = false;
        } else if (titletype == "2") {
            $scope.showComplex = false;
        } else if (titletype == "3") {
            $scope.autnotice = false;
        }
    }
    //离职人员编辑框
    $scope.showLeaveInfo = false;
    $scope.userInfoLeave = {};

    $scope.currUserId = "";
    //用户编辑信息
    $scope.useredit = function (userobj) {

        $scope.userInfoLeave = JSON.parse(userobj.dimissionUserJson);
        $scope.currUserId = userobj.dimissionId;
        $scope.showLeaveInfo = true;


    }
    //关闭窗口
    $scope.closeuser = function () {
        $scope.showLeaveInfo = false;
    }
    //保存用户
    $scope.saveUser = function () {
        RS.modifyDimissionUserInfo({ dimissionUserJson: $scope.userInfoLeave, dimissionId: $scope.currUserId, loginUserId: sysUserInfo.getuserinfo().user_ID }).then(function (data) {
            if (data.errorcode == 0) {
                Msg.success("修改成功！");
                $scope.showLeaveInfo = false;
                $scope.GetUserList("", 0);
            } else {
                Msg.error("修改失败！");
            }

        }, function errorCallback() {
            console.log("连接服务器失败！");
        });
    }
} ]);
